jQuery(document).ready(function($) {
    var cookie = JSON.parse($.cookie('bjou_student'));
	$.ajax({
		type: 'GET',
		url: 'api/v1/check'

	}).done(function (data, textStatus, jqXHR) {

	}).fail(function (jqXHR, textStatus, errorThrown) {
        window.location = '../index.html';
	});
    $('#welcome').html("欢迎," + cookie.studentName + "同学")
	$('#logout').on('click', function (event) {
		event.preventDefault();
		$.ajax({
			data: {},
			timeout: 1000,
			type: 'POST',
			url: '../logout'

		}).done(function(data, textStatus, jqXHR) {
			window.location = '../index.html';
		}).fail(function(jqXHR, textStatus, errorThrown) {
		    window.location = '../index.html';
		});
	});


});


var cookie = JSON.parse($.cookie('bjou_student'));
var getStudentPlanDetailsUrl = 'api/v1/plan/details/' + cookie.studentID;
var getStudentPlanDetailsOptions = {
    elem: '#StudentPlanTable',
    url: getStudentPlanDetailsUrl,
    method: 'POST',
    contentType: 'application/json',
    id: 'studentPlanDetailsTable',
    loading: true,
    cols: [[
         {field:'mkm', title:'模块'}
        , {field:'mkxf', title:'最低学分'}
        , {field:'mkzyxf', title:'中央最低学分'}
        , {field:'tgzf', title:'模块通过学分'}
        , {field:'kcid', title:'课程ID'}
        , {field:'kcmc', title:'课程名称'}
        , {field:'kcxzmc', title:'课程性质'}
        , {field:'kclxmc', title:'课程类型'}
        , {field:'xf', title:'学分'}
        , {field:'ksxq', title:'建议开设学期'}
        , {field:'ksdwlxmc', title:'考试单位'}
        , {field:'zhcj', title:'成绩'}
    ]],
    request: {
        limitName: 'size'
    },
    response: {
        statusName: null,
        statusCode: null,
        dataName: 'collections'
    },
    done: function(res, curr, count) {
        var data = res.collections;
        var mergeIndex = 0;//定位需要添加合并属性的行数
        var mark = 1; //这里涉及到简单的运算，mark是计算每次需要合并的格子数
        var columsName = ['mkm','mkxf','mkzyxf','tgzf'];//需要合并的列名称
        var columsIndex = [0,1,2,3];//需要合并的列索引值

        var preBreakpoint = [];
        for (var k = 0; k < columsName.length; k++) { //这里循环所有要合并的列
            var trArr = $(".layui-table-body>.layui-table").find("tr");//所有行
            for (var i = 1; i < data.length; i++) { //这里循环表格当前的数据
                var tdCurArr = trArr.eq(i).find("td").eq(columsIndex[k]);//获取当前行的当前列
                var tdPreArr = trArr.eq(mergeIndex).find("td").eq(columsIndex[k]);//获取相同列的第一列

                if ((data[i][columsName[k]] === data[i-1][columsName[k]]) && (!preBreakpoint.includes(i))) { //后一行的值与前一行的值做比较，相同就需要合并
                    mark += 1;
                    tdPreArr.each(function () {//相同列的第一列增加rowspan属性
                        $(this).attr("rowspan", mark);
                    });
                    tdCurArr.each(function () {//当前行隐藏
                        $(this).css("display", "none");
                    });
                } else {
                    mergeIndex = i;
                    preBreakpoint.push(mergeIndex)
                    mark = 1;//一旦前后两行的值不一样了，那么需要合并的格子数mark就需要重新计算
                }
            }
            mergeIndex = 0;
            mark = 1;
        }
        var zxf = data.length > 0 ?  data[0].zxf : 0.0
        $('#zxf').html(zxf)
    }
};
layui.use(['table', 'form', 'element'], function(){
    var $ = layui.jquery
    ,table = layui.table
    ,form =layui.form
    ,element = layui.element;

    $.ajax({
        type: 'GET',
        url: 'api/v1/plan/brief/' + cookie.studentID
    }).done(function (data, textStatus, jqXHR) {
        $("#xh").html(data.xh)
        $("#xm").html(data.xm)
        $("#zymc").html(data.zymc)
        $("#gzh").html(data.gzh)
        $("#zyccmc").html(data.zyccmc)
        $("#xslxmc").html(data.xslxmc)
        $("#zygznd").html(data.zygznd)
        $("#zygzxqmc").html(data.zygzxqmc)
        $("#byxf").html(data.byxf)
        $("#zyddksxf").html(data.zyddksxf)
    }).fail(function (jqXHR, textStatus, errorThrown) {
    });
    table.render(getStudentPlanDetailsOptions);

});